# -*- coding: utf-8 -*-
import socket
import threading

# 监听的IP及端口
bind_ip = "127.0.0.1"
bind_port = 9999
bufz =1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print("[*] Listening on %s:%d" % (bind_ip, bind_port))

# def handle_client(client_socket):
#     request = client_socket.recv(bufz).decode()
#     print("[*] Received: %s" % request)
#     client_socket.send("ok!".encode('utf8'))
#     # client_socket.close()

while True:
    print("等待链接...")
    client, addr = server.accept()
    print('已连接 ',addr[0])

    # client_handler = threading.Thread(target=handle_client, args=(client,))
    # client_handler.start()

    while True:
        data = client.recv(bufz).decode()
        print('接收到数据-> ',data)
        if not data:
            break
        client.send('服务器回应'.encode('utf8'))
    client.close()
server.close()
